/*
 * Copyright (c) 2024 -present FengJinjian ( 751965763@qq.com )
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.vital.energy.longevity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.vital.energy.longevity.domain.dto.order.OrderCreateDTO;
import edu.vital.energy.longevity.domain.dto.order.OrderQueryDTO;
import edu.vital.energy.longevity.domain.dto.order.OrderUpdateDTO;
import edu.vital.energy.longevity.domain.entity.Order;
import edu.vital.energy.longevity.domain.entity.User;
import edu.vital.energy.longevity.mapper.OrderMapper;
import edu.vital.energy.longevity.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务实现类
 *
 * @author Feng JinJian
 * @version 1.0.1
 * @date 2025-03-19
 */
@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Map<String, Object> getOrderPage(Integer pageNum, Integer pageSize, OrderQueryDTO query) {
        // 构建查询条件
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();

        if (query.orderNumber()!= null) {
            wrapper.like(Order::getOrderNumber, query.orderNumber());
        }
        if (query.userId() != null) {
            wrapper.eq(Order::getUserId, query.userId());
        }
        System.out.println(pageNum+"---"+pageSize);
        // 执行分页查询
        IPage<Order> page = orderMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);

        // 包装返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("records", page.getRecords());
        result.put("current", page.getCurrent());
        result.put("size", page.getSize());
        result.put("total", page.getTotal());
        result.put("pages", page.getPages());

        return result;
    }

    @Override
    public Order getOrderById(Long id) {
        Order order = orderMapper.selectById(id);
        if (order == null) {
            throw new IllegalArgumentException("不存在");
        }
        return order;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createOrder(OrderCreateDTO dto) {
        Order order = new Order();
        BeanUtils.copyProperties(dto, order);

        orderMapper.insert(order);
        log.info("创建成功: id={}", order.getId());

        return order.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateOrder(Long id, OrderUpdateDTO dto) {
        Order order = getOrderById(id);
        BeanUtils.copyProperties(dto, order);

        int rows = orderMapper.updateById(order);
        log.info("更新成功: id={}, 影响行数={}", id, rows);
        return rows;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteOrder(Long id) {
        int rows = orderMapper.deleteById(id);
        if (rows <= 0) {
            throw new IllegalArgumentException("不存在");
        }
        log.info("删除成功: id={}, 影响行数={}", id, rows);
        return rows;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchDeleteOrder(List<Long> ids) {
        int rows = orderMapper.deleteBatchIds(ids);
        log.info("批量删除成功: ids={}, 影响行数={}", ids, rows);
        return rows;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOrderStatus(Long id, Integer status) {
        Order order = getOrderById(id);

        orderMapper.updateById(order);
        log.info("更新状态成功: id={}, status={}", id, status);
    }
}
